/* 一 */
/*  */
/* function curry1(fn){
    // 将伪数组arguments转化成数组
    const args = [].slice.call(arguments,1), length = fn.length
    return function inner(){
        const newArgs = args.concat([].slice.call(arguments))
        return fn.apply(this,newArgs)
    }
}
const add = curry1(function () {
    const args = Array.prototype.slice.call(arguments)
    const sum = args.reduce((acc, cur) => acc+cur, 0)
    return sum
})
console.log(add(1,2,3)) */
/* 二 */
/* function sub_curry2(fn){
    let args = [].slice.call(arguments,1)
    return function(){
        return fn.apply(this, args.concat([].slice.call(arguments)))
    }
} */
/* function curry3(fn,length){
    let length = length || fn.length
    let slice = Array.prototype.slice
    return function(){
        if(arguments.length <length){
            let combined = [fn].concat(slice.call(arguments))
            return curry(sub_curry.apply(this,combined),length-arguments.length)
        }else{
            return fn.apply(this,arguments)
        }
    }
} */


/* function curry1 (fn) {
    const _argus = []
    const length = fn.length // 条件
    return (function inner(...argus){
      if(args.length !== 0){
        _argus.push(...argus)
      }
      const that = this
      return args.length >= length ? fn.apply(that, _argus) : (...args)=>{inner.apply(that,args)}
      //或 return args.length >= length ? fn.apply(that, _argus) : inner.bind(that)
    })()
  }


function curry2 (fn, ...argus) {
    const length = fn.length;
    const that = this
    return argus.length >= length ? fn.apply(that, argus) : (...args)=>curry2.call(that, fn,...argus, ...args)
}
// 测试
//   箭头函数内不能使用arguments
const add = curry1(function (a, b, c) {
    const args = [].slice.call(arguments)
    return args.reduce((acc, cur) => acc + cur, 0)
})

const add1 = add(1, 2)
console.log(add1(3)) */

// 题目
// calculate(1)(2)('+')
// 一、单个单个参数x传递
/* function curring1 (fn, ...args) {
    return function (x) {
        const that = this
        return x==='+' ? fn.apply(that, args) : curring1.call(that,fn, x,...args)
    }
}
const calculate1 = curring1(function(...args){
    return args.reduce((acc, cur)=>acc+cur,0)
})
console.log(calculate1(1)(2)('+')) */
// 二、分批参数传递
/* function curring2 (fn) {
    return function inner(...args) {
        const argus = [].slice.call(arguments)
        const length = argus.length
        const that = this
        if (length !== 0 && argus[length-1] === '+'){
            return fn.apply(that, argus.slice(0,length-1))
        } 
        return (...ars) => inner.call(that, ...args, ...ars)
    }
}
const calculate2 = curring2(function (...args) {
    return args.reduce((acc, cur) => acc+cur, 0)
}) 
console.log(calculate2(1,2)(3)(4, '+')) */
